extends Node
func _ready():
	pass
func _init():
	call_deferred("init")
	pass
func _connect():
	pass
var utils = null
var azurCtrl = null
var base = null
var jbConfig
func init():
	if globalData.infoDs.has("g_azurlaneBase"):
		base = globalData.infoDs["g_azurlaneBase"]
	if base != null:
		#初始化
		utils = globalData.infoDs["g_azurlaneUtils"]
		azurCtrl = globalData.infoDs["g_azurlane"]
		yield(sys.get_tree().create_timer(0.5), "timeout")
		jbConfig = load("%s/g_azurlaneJiban/jibanConfig.gd" % [sys.getBasePath()]).new()

func onBattleEnd():
	if sup1Flag:
		sys.main.player.plusGold(15)

func onBattleStart():
	sup1Flag = false
	var jbDic = checkJB()
	for config in jbDic.keys():
		var num = jbDic.get(config)
		for cha in sys.main.getAllChas(2):
			if num >= config.num1:
				cha.addBuff(jbConfig.loadBuff(config.buff1))
			if config.num2 != null and num >= config.num2:
				cha.addBuff(jbConfig.loadBuff(config.buff2))
var sup1Flag = false
func checkJB():
	var chas = []
	var jbDic = {}
	var checkedChara1 = []
	var checkedChara2 = []
	var checkedChara3 = []
	for i in jbConfig.jbs:
		for cha in sys.main.getAllChas(2):
			if i.type == "shipClass" and cha.type == i.bind and not checkedChara1.has(cha.ename):
				utils.dicPlus(jbDic, i, 1)
				checkedChara1.append(cha.ename)
			if i.type == "camp" and cha.camp == i.bind and not checkedChara2.has(cha.ename):
				utils.dicPlus(jbDic, i, 1)
				checkedChara2.append(cha.ename)
			if i.type == "chara" and i.bind.has(cha.ename) and not checkedChara3.has(cha.ename):
				utils.dicPlus(jbDic, i, 1)
				checkedChara3.append(cha.ename)

	return jbDic

func show():
	if sys.main.get_node("ui").has_node("jiban") and is_instance_valid(sys.main.get_node("ui/jiban")):
		sys.main.get_node("ui/jiban").queue_free()
	var panel = utils.createWindowDialog(sys.main.get_node("ui"), "当前羁绊", Vector2(700, 600))
	panel.name = "jiban"
	panel.popup_centered()

	var jbDic = checkJB()

	var text = ""

	for config in jbDic.keys():
		var num = jbDic.get(config)

		var color1 = "[color=#C0C0C0]"
		var color2 = "[color=#C0C0C0]" 

		if num >= config.num1:
			color1 = "[color=#DC143C]"
		if config.num2 != null and num >= config.num2:
			color2 = "[color=#DC143C]"

		text += "[color=#33ff33]%s[/color] - 当前：[color=#DC143C]%d[/color]\n"%[config.name, num]
		text += "[color=#BDB76B]%d[/color] - %s %s[/color]\n"%[config.num1, color1, config.text1]
		if config.num2 != null:
			text += "[color=#BDB76B]%d[/color] - %s %s[/color]\n"%[config.num2, color2, config.text2]

		text += "\n"

	utils.createRichTextLabel(text, panel, Vector2(600, 500), Vector2(50, 50))